package cn.rongdao.jrkabao.ui.main.mainfragment.cashadvance.ui;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;

import com.mifengkong.frtools.util.FRString;
import com.mifengkong.frwidget.banner.customerbanner.onTouchClickListener;

import java.util.List;

import cn.rongdao.jrkabao.R;
import cn.rongdao.jrkabao.bean.main.banner.BannerBean;
import cn.rongdao.jrkabao.ui.main.mainfragment.uicomponent.BannerUIComponent;
import cn.rongdao.jrkabao.ui.webview.WebViewActivity;
import cn.rongdao.jrkabao.util.LoginUtil;
import cn.rongdao.jrkabao.util.growingio.BuriedUtil;

/**
 * Created by WANG on 17/12/19
 */

public class BannerComponent extends LinearLayout {
    public BannerComponent(Context context) {
        super(context);
    }

    public BannerComponent(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public BannerComponent(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private BannerUIComponent bannerUIComponent;
    private List<BannerBean> bannerBeanList;
    private View line;

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        bannerUIComponent = findViewById(R.id.home_banner);
        bannerUIComponent.setIndicatorRectangle(true);
        line = findViewById(R.id.home_banner_line);
        initListener();
    }

    private void initListener() {
        bannerUIComponent.setOnItemClickListener(new onTouchClickListener() {
            @Override
            public void onItemTouchClickListener(int position) {
                if (null != bannerBeanList && bannerBeanList.size() > 0) {
                    String link = bannerBeanList.get(position).getAd_link();
                    BuriedUtil.kamao_shouye_banner_apply(getContext(), link);
                    if (!LoginUtil.isLogin(getContext())) {
                        return;
                    }
                    if (!FRString.isEmpty(link)) {
                        WebViewActivity.start(getContext(), bannerBeanList.get(position).getAd_link());
                    }
                }
            }
        });
    }

    public void setBannerData(List<BannerBean> bannerData) {
        bannerBeanList = bannerData;
        bannerUIComponent.setBannerList(bannerData);
    }

    public void setOwnVisible(boolean ownVisible) {
        if (ownVisible) {
            bannerUIComponent.setVisibility(VISIBLE);
            line.setVisibility(VISIBLE);
        } else {
            bannerUIComponent.setVisibility(GONE);
            line.setVisibility(GONE);
        }
    }
}
